package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author gcq
 * @Create 2020-11-20
 */
@Configuration
public class MyMQconfig {

//    @RabbitListener(queues = "stock.release.stock.queue")
//    public void listener(Message message) {
//
//    }

//    /**
//     * 使用JSON序列化机制，进行消息转换
//     * @return
//     */
//    @Bean
//    public Jackson2JsonMessageConverter messageConverter() {
//        return new Jackson2JsonMessageConverter();
//    }



    /**
     * 使用JSON序列化机制，进行消息的转换
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    @Bean
    public Exchange stockEventExchange() {
        return new TopicExchange("stock-event-exchange"
                ,true
                ,false);
    }
    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue"
                ,true
                ,false
                ,false);
    }

    /**
     * 创建队列
     * @return
     */
    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> map = new HashMap<>();
        // 指定交换器
        map.put("x-dead-letter-exchange", "stock-event-exchange");
        // 路由key
        map.put("x-dead-letter-routing-key", "stock.release");
        // 超时时间
        map.put("x-message-ttl", 180000); // 3分钟
        return new Queue("stock.delay.queue",true,false,false,map);
    }

    /**
     * release.stock 与 stock.event进行绑定
     * @return
     */
    @Bean
    public Binding stockReleaseBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null);
   }

    /**
     * delay 与 event-exchange 进行绑定
     * @return
     */
    @Bean
    public Binding stockLockBinding() {
            return new Binding("stock.delay.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.locked",
                null);
    }


}